package com.roamer.javase.demo.algorithm;

/**
 * 找寻前K个数字
 *
 * @author roamer
 * @version v1.0
 * @date 2019/9/21 17:32
 */
public class QueryMinNumber {

    public static int[] booleanFilter(int[] nums, int k) {
        if (null == nums || nums.length == 0 || k < 0 || nums.length < k) {
            throw new IllegalArgumentException("请检查参数");
        }
        // 1.寻找最大值
        int max = nums[0];
        for (int i = 1; i < nums.length; i++) {
            if (max < nums[i]) {
                max = nums[i];
            }
        }
        // 2.创建布隆数组
        int[] booleanArray = new int[max + 1];
        for (int num : nums) {
            booleanArray[num] = ++booleanArray[num];
        }
        // 3.获取前K个值
        int[] resultArray = new int[k];
        for (int i = 0, count = 0; i < booleanArray.length && count < k; i++) {
            for (int j = 0; j < booleanArray[i] && count < k; j++) {
                resultArray[count++] = i;
            }
        }
        return resultArray;
    }
}
